﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using System.Web.Security;

namespace Framework.WebmasterTool.Memberships
{
    public partial class ResetPassword : System.Web.UI.Page
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermission();
                MembershipUser someUser = Membership.GetUser();

                if (someUser == null || (someUser.UserName != "admin" && someUser.UserName != "administrator"))
                {
                    lblPermissionMessage.Visible = true;
                    Panel1.Visible = false;
                }
            }
        }

        public void CheckPermission()
        {
            MembershipUser someUser = Membership.GetUser();
            if (someUser == null)
            {
                Response.Redirect(Globals.ApplicationPath + "LoginAdmin/Default.aspx");
            }
            bool flag = false;
            var user = entity.aspnet_Users.Where(us => us.UserName == someUser.UserName).FirstOrDefault();
            user.aspnet_Roles.Load();
            var rolesForUser = user.aspnet_Roles.ToList();
            foreach (var item in rolesForUser)
            {
                var permission = entity.udv_Permissions.Where(ps => ps.RoleId == item.RoleId).FirstOrDefault();
                if (permission.ManagerUser.Value == true)
                {
                    flag = true;
                }
            }
            if (flag == false)
            {
                lblPermissionMessage.Visible = true;
                Panel1.Visible = false;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            MembershipUser someUser = Membership.GetUser(txtUserName.Text.Trim());
            if (someUser != null)
            {
                if (someUser.IsLockedOut == true)
                {
                    someUser.UnlockUser();
                }
                if (someUser.ChangePassword(someUser.ResetPassword(txtPassword.Text), txtPassword.Text))
                {
                    string Username = txtUserName.Text.Trim();
                    txtUserName.Text = string.Empty;
                    lblMsg.Text = "Lấy lại mật cho " + Username + " thành công";
                    lblMsg.ForeColor = System.Drawing.Color.Blue;

                    //create db log
                    CreateDBLog(null, null, TypeAction.Update, "Reset mật khẩu cho user: " + Username, "", "", "", "",
                        "", "", "", "");
                }
            }
            else
            {
                lblMsg.Text = "Tên đăng nhập này không tồn tại trong hệ thống";
                lblMsg.ForeColor = System.Drawing.Color.Red;
            }
        }

        public void CreateDBLog(int? postID, int? postCateID, TypeAction typeAction, string title, string _abstract, string description,
        string releaseDate, string expireDate, string isApproved, string statusID, string metaDescription, string metaKeywords)
        {
            udv_Log log = new udv_Log();
            log.PostID = postID;
            log.PostCateID = postCateID;
            log.TypeAction = (int)typeAction;
            log.Title = title;
            log.Abstract = _abstract;
            log.Description = description;
            log.ReleaseDate = releaseDate;
            log.ExpireDate = expireDate;
            log.IsApproved = isApproved;
            log.StatusID = statusID;
            log.MetaDescription = metaDescription;
            log.MetaKeywords = metaKeywords;
            log.ActionedDate = DateTime.Now;
            log.ActionBy = Membership.GetUser().UserName;
            log.IpAdress = Globals.GetClientIPAddress();

            entity.AddToudv_Log(log);
            entity.SaveChanges();
        }
    }
}